class LastJiFenView extends egret.DisplayObjectContainer{

    private _bgMask:egret.Shape; // 背景幕布
    private _contentView:egret.DisplayObjectContainer; // content
    private _bg:egret.Bitmap; // 背景
    private _tabbar:LastJiFenTabbar; // 切换按钮
    private _personTableView:TableView.TableView; // 个人积分榜
    private _bangTableView:TableView.TableView; // 帮会积分榜
    private _timeLabel:BaseTextField; // 显示下次活动时间
    private _cancelBtn:TbitMap; // 关闭按钮
    private _titleLabel:BaseTextField; // 标题
    private _todayLabel:BaseTextField; // 上期时间
    private _todayShape:egret.Shape; // 上期时间背景
    private _personLoading:boolean = false; 
    private _bangLoading:boolean = false;
    private _emptyLabel:EmptyView; // 显示空
    private _currentIndex:number; // 当前位置

    constructor(){
        super();
        this.width = GameData.stageW;
        this.height = GameData.stageH;
        this.touchEnabled = true;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.ViewDidLoad, this);
    }

    private ViewDidLoad(){
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.viewDidDisappear, this);
        // 赋初值
        this.init();
        // 1.背景幕布
        if(!this.contains(this.bgMask)) this.addChild(this.bgMask);
        // 2.content
        if(!this.contains(this.contentView)) this.addChild(this.contentView);
        // 3.背景
        if(!this.contentView.contains(this.bg)) this.contentView.addChild(this.bg);
        // 4.tabbar
        if(!this.contentView.contains(this.tabbar)) this.contentView.addChild(this.tabbar);
        // 5.个人积分榜
        if(!this.contentView.contains(this.personTableView)) this.contentView.addChild(this.personTableView);
        // 6.帮会积分榜
        if(!this.contentView.contains(this.bangTableView)) this.contentView.addChild(this.bangTableView);
        // 7.下次活动时间
        if(!this.contentView.contains(this.timeLabel)) this.contentView.addChild(this.timeLabel);
        // 8.关闭按钮
        if(!this.contentView.contains(this.cancelBtn)) this.contentView.addChild(this.cancelBtn);
        // 9.标题
        if(!this.contentView.contains(this.titleLabel)) this.contentView.addChild(this.titleLabel);
        // 10.上期时间背景
        if(!this.contentView.contains(this.todayShape)) this.contentView.addChild(this.todayShape);
        // 11.上期时间
        if(!this.contentView.contains(this.todayLabel)) this.contentView.addChild(this.todayLabel);
        // 12.空
        if(!this.contentView.contains(this.emptyLabel)) this.contentView.addChild(this.emptyLabel);
        
        // 添加动画
        this.addAnimation();
    }

    private viewDidDisappear(){
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.viewDidDisappear, this);
        
        this.personTableView.dataSource = [];
        this.bangTableView.dataSource = [];
        this._personLoading = false;
        this._bangLoading = false;
    }

    /*********************************************** private **********************************************************/
    private tabbarChangeAction(index:number){
        this.bangTableView.visible  = index == 1;
        this.personTableView.visible = !this.bangTableView.visible;
        this._currentIndex = index;
        switch(index){
            case 0:
                this.queryPersonData();
                break;
            case 1:
                this.queryBangData();
                break;
        }
    }

    private init(){
        this.tabbar.index = 0;
        this.personTableView.visible = this.bangTableView.visible = false;
    }

    private cancelAction(){
        egret.Tween.removeTweens(this.contentView);
        egret.Tween.get(this.contentView).to({scaleX:0, scaleY:0}, 100).call(function(){
            if(this.parent) this.parent.removeChild(this);
        }.bind(this));
    }

    // 请求个人排行数据
    private queryPersonData(){
        if(this._personLoading){
            this.showEmpty(false, 0);
            return;
        }
        this._personLoading = true;
        var self = this;
        //?action=boxlastpersonrank
        CurlUtil.curl(config.api_url + '/bxg/rank/personal/last', {}).then((data)=>{
            if(self._currentIndex != 0){
                self._personLoading = false;
                return;
            }
            if(data['status'] == "success"){
                data = data['data']; 
                // 更新时间显示
                self.updateDate(data['next_time'], data['date']);
                // 刷新tableView数据源
                var dataSource = data['list'] || [];
                if(dataSource.length > 0){
                    self.personTableView.dataSource = dataSource;
                }else{
                    self._personLoading = false;
                }
                // 显示空
                self.showEmpty(dataSource.length == 0, 0);
            }else{
                self._personLoading = false;
                self.showEmpty(true, 0);
                self.updateDate('', '');
            }
        }).catch((data)=>{
            self._personLoading = false;
            self.showEmpty(true, 0);
            self.updateDate('', '');
        });
    }

    // 请求帮派数据
    private queryBangData(){
        if(this._bangLoading){
            this.showEmpty(false, 1);
            return;
        }
        this._bangLoading = true;
        var self = this;
        //?action=boxlastbangrank 
        CurlUtil.curl(config.api_url + '/bxg/rank/gang/last', {}).then((data)=>{
            if(self._currentIndex != 1){
                self._bangLoading = false;
                return;
            }
            if(data['status'] == "success"){
                data = data['data'];
                // 更新时间显示
                self.updateDate(data['next_time'], data['date']);
                // 刷新tableView数据源
                var dataSource = data['list'] || [];
                if(dataSource.length > 0){
                    self.bangTableView.dataSource = dataSource;
                }else{
                    self._bangLoading = false;
                }
                self.showEmpty(dataSource.length == 0, 1);
            }else{
                self._bangLoading = false;
                self.showEmpty(true, 0);
                self.updateDate('', '');
            }
        }).catch((data)=>{
            self._bangLoading = false;
            self.showEmpty(true, 0);
            self.updateDate('', '');
        });
    }

    // 更新时间
    private updateDate(next:string, date:string){
        if(next){
            this.timeLabel.text = "下次活动开启时间\n" +next;
        }else{
            this.timeLabel.text = '';
        }
        if(date){
            this.todayLabel.text = date;
            this.todayShape.visible = true;
            this.titleLabel.y = 122;
        }else{
            this.todayLabel.text = '';
            this.todayShape.visible = false;
            this.titleLabel.y = 140;
        }
    }

    // 添加动画
    private addAnimation(){
        this.contentView.scaleX = this.contentView.scaleY = 0;
        egret.Tween.removeTweens(this.contentView);
        egret.Tween.get(this.contentView).to({scaleX:1, scaleY:1}, 500, egret.Ease.backOut);
    }

    // 空数据
    private showEmpty(show:boolean, index:number){
        this.emptyLabel.visible = show;
        this.personTableView.visible = this.bangTableView.visible = false;
        if(show) return;
        switch(index){
            case 0:
                this.personTableView.visible = true;
                break;
            case 1:
                this.bangTableView.visible = true;
                break;
        }
    }

    /*********************************************** getter **********************************************************/
    
    private get emptyLabel():EmptyView{
        if(!this._emptyLabel){
            var emptyLabel = new EmptyView();
            emptyLabel.width = this.width;
            emptyLabel.y = utils.getMaxY(this.tabbar) + 150;
            emptyLabel.visible = false;
            this._emptyLabel = emptyLabel;
        }
        return this._emptyLabel;
    }
    
    public get todayShape():egret.Shape{
        if(!this._todayShape){
            var todayShape = new egret.Shape();
            todayShape.x = this.todayLabel.x;
            todayShape.y  = this.todayLabel.y;
            todayShape.width  = this.todayLabel.width;
            todayShape.height = this.todayLabel.height;
            todayShape.graphics.beginFill(0x7B490D);
            todayShape.graphics.drawRoundRect(0, 0, todayShape.width, todayShape.height, 20);
            todayShape.graphics.endFill();
            this._todayShape = todayShape;
        }
        return this._todayShape;
    }

    public get todayLabel():BaseTextField{
        if(!this._todayLabel){
            var todayLabel = new BaseTextField();
            todayLabel.textColor = 0xFFF894;
            todayLabel.text = "";
            todayLabel.width = 188;
            todayLabel.height = 35;
            todayLabel.size = 26;
            todayLabel.textAlign = egret.HorizontalAlign.CENTER;
            todayLabel.verticalAlign = egret.VerticalAlign.MIDDLE;
            todayLabel.x = (this.contentView.width - todayLabel.width) / 2.0;
            todayLabel.y = utils.getMaxY(this.titleLabel) + 10;
            this._todayLabel = todayLabel;
        }
        return this._todayLabel;
    }

    public get titleLabel():BaseTextField{
        if(!this._titleLabel){
            var titleLabel = new BaseTextField();
            titleLabel.text = "上期奖励分配";
            titleLabel.x = this.bgX;
            titleLabel.width = this.contentView.width - 2 * titleLabel.x;
            titleLabel.y = 122;
            titleLabel.textAlign = egret.HorizontalAlign.CENTER;
            titleLabel.stroke = 4;
            titleLabel.strokeColor = 0xBA7114;
            titleLabel.textColor = 0xF2E019;
            titleLabel.bold = true;
            titleLabel.size = 38;
            this._titleLabel = titleLabel;
        }
        return this._titleLabel;
    }

    public get cancelBtn():TbitMap{
        if(!this._cancelBtn){
            var cancelBtn = utils.createBitmapByName("x", {offsetX:0.5, offsetY:0.5});
            cancelBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.cancelAction, this);
            cancelBtn.touchAnimation = true;
            cancelBtn.x = this.contentView.width - 100;
            cancelBtn.y = 165;
            this._cancelBtn = cancelBtn;
        }
        return this._cancelBtn;
    }

    public get timeLabel():BaseTextField{
        if(!this._timeLabel){
            var timeLabel = new BaseTextField();
            timeLabel.height = 110;
            timeLabel.textAlign = egret.HorizontalAlign.CENTER;
            timeLabel.textColor = 0x7C552C;
            timeLabel.lineSpacing  = 10;
            timeLabel.x = this.bgX;
            timeLabel.width = (this.contentView.width -  2 * timeLabel.x);
            timeLabel.y = this.contentView.height - timeLabel.height - 50;
            timeLabel.text = "下次活动开启时间\n";
            timeLabel.verticalAlign = egret.VerticalAlign.MIDDLE;
            timeLabel.size = 26;
            timeLabel.bold = true;
            this._timeLabel = timeLabel;
        }
        return this._timeLabel;
    }

    public get bangTableView():TableView.TableView{
        if(!this._bangTableView){
            var bangTableView = new TableView.TableView();
            bangTableView.x = this.bgX + 20;
            bangTableView.width = Math.floor(this.contentView.width - 2 * bangTableView.x);
            bangTableView.y = utils.getMaxY(this.tabbar);
            bangTableView.height = this.timeLabel.y - bangTableView.y;
            bangTableView.visible = false;
            bangTableView.itemRenderer = LastJiFenBangCell;
            LastJiFenBangCell.WIDTH = bangTableView.width;
            this._bangTableView = bangTableView;
        }
        return this._bangTableView;
    }

    public get personTableView():TableView.TableView{
        if(!this._personTableView){
            var personTableView = new TableView.TableView();
            personTableView.x = this.bgX + 20;
            personTableView.width = Math.floor(this.contentView.width - 2 * personTableView.x);
            personTableView.y = utils.getMaxY(this.tabbar);
            personTableView.height = this.timeLabel.y - personTableView.y;
            personTableView.visible = false;
            personTableView.itemRenderer = LastJiFenPersonCell;
            LastJiFenPersonCell.WIDTH = personTableView.width;
            this._personTableView = personTableView;
        }
        return this._personTableView;
    }

    public get tabbar():LastJiFenTabbar{
        if(!this._tabbar){
            var tabbar = new LastJiFenTabbar();
            var items = ["个人积分榜", "帮会积分榜"];
            tabbar.items = items;
            tabbar.callback = this.tabbarChangeAction.bind(this);
            // tabbar.width = this.contentView.width - 2 * this.bgX;
            tabbar.width = 190 * items.length + 20 * (items.length - 1);
            tabbar.height = 80;
            tabbar.x = this.bgX + 20;
            tabbar.y = 230;
            this._tabbar = tabbar;
        }
        return this._tabbar;
    }

    private get bgX():number{
        return 56 * this.bg.width / 600 + 10;
    }

    public get bg():egret.Bitmap{
        if(!this._bg){
            var bg = utils.createBitmapByName('djc_bg', {width:this.contentView.width});
            bg.scale9Grid = new egret.Rectangle(0, 244, bg.width, 250);
            bg.height = this.contentView.height;
            bg.x = (this.contentView.width - bg.width) / 2.0 + 10;
            bg.y = (this.contentView.height - bg.height) / 2.0;
            this._bg = bg;
        }
        return this._bg;
    }

    public get contentView():egret.DisplayObjectContainer{
        if(!this._contentView){
            var contentView = new egret.DisplayObjectContainer();
            contentView.width = this.width;
            contentView.height = this.height * 0.9;
            contentView.anchorOffsetX = contentView.width / 2.0;
            contentView.anchorOffsetY = contentView.height / 2.0;
            contentView.x = (this.width - contentView.width) / 2.0 + contentView.anchorOffsetX;
            contentView.y = (this.height - contentView.height) / 2.0 + contentView.anchorOffsetY;
            this._contentView = contentView;
        }
        return this._contentView;
    }

    public get bgMask():egret.Shape{
        if(!this._bgMask){
            var bgMask = new egret.Shape();
            bgMask.graphics.beginFill(0x000000, 0.7);
            bgMask.graphics.drawRect(0, 0, GameData.stageW, GameData.stageH);
            bgMask.graphics.endFill();
            this._bgMask = bgMask;
        }
        return this._bgMask;
    }
}